package org.example.test.five;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.example.test.util.RabbitMQUtils;

//消息接收01
public class ReceiveLogs02 {
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();
        // 声明一个交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //声明一个队列
        /*
         * 生成一个临时队列，队列的名称是随机
         * 当消费者断开与队列的连接时，该队列会自动删除
         */
        String queueName = channel.queueDeclare().getQueue();//临时队列
        channel.queueBind(queueName, EXCHANGE_NAME, "");//绑定队列到交换机
        System.out.println("等待接收消息,把接收到的消息打印在屏幕上");
        // 消息接收
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("ReceiveLogs02接收到的消息为：" + new String(message.getBody(),"UTF-8"));
        };
        //消费者取消息时回调接口
        channel.basicConsume(queueName, true, deliverCallback,consumerTag -> {});
    }
}
